package com.servlet.control;

import com.bean.Course;
import com.jdbc.util.DBUtilForTeacher;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "AddTeacherCourseServlet", urlPatterns = "/AddTeacherCourseServlet")
public class AddTeacherCourseServlet extends HttpServlet {
	public AddTeacherCourseServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		DBUtilForTeacher db = new DBUtilForTeacher();
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=utf-8");

		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		db.getConn();

		String courseId = request.getParameter("courseID");
		String courseName = request.getParameter("courseName");
		String teacher = request.getParameter("teacher");
		int point = Integer.parseInt(request.getParameter("point"));
		// System.out.println(point);
		String time_1 =
				request.getParameter("time_1_weekday") + request.getParameter("time_1_timeFrame");
		String time_2 =
				request.getParameter("time_2_weekday") + request.getParameter("time_2_timeFrame");
		String location = request.getParameter("location");
		int limited = Integer.parseInt(request.getParameter("limited"));
		// System.out.println(limited);

		Course course =
				new Course(courseId, courseName, teacher, point, time_1, time_2, location, limited);

		int flag = 0;

		flag = db.insert("insert into course values(?,?,?,?,?,?,?,?)", course);
		System.out.println("flag =" + flag);
		session.setAttribute("flag", flag);
		try {
			response.sendRedirect(request.getContextPath() + "/jsp/AddTeacherCourse.jsp");
			// request.getRequestDispatcher("/jsp/AddTeacherCourse.jsp").forward(request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		db.release();
	}
}
